﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace ERP.Classes
{
    public class cs_erp_chitiet_nhapkhoInfo
    {
        private int _id_chitiet_nhapkho;
        private int _id_lannhap;
        private int _mahh;
        private double _soluong;
        private DateTime _ngaynhap;
        private double _dongia;
        private double _thanhtien;
        private int _nhan;
        private int _makho;
        private bool _hangdu;

        public cs_erp_chitiet_nhapkhoInfo()
        {
            this._id_chitiet_nhapkho = 0;
            this._id_lannhap = 0;
            this._mahh = 0;
            this._soluong = 0;
            this._ngaynhap = DateTime.Now;
            this._dongia = 0;
            this._thanhtien = 0;
            this._nhan = 0;
            this._makho = 0;
            this._hangdu = false;

        }
        public int id_chitiet_nhapkho
        {
            get
            {
                return this._id_chitiet_nhapkho;
            }
            set
            {
                this._id_chitiet_nhapkho = value;
            }
        }
        public int id_lannhap
        {
            get
            {
                return this._id_lannhap;
            }
            set
            {
                this._id_lannhap = value;
            }
        }
        public int mahh
        {
            get
            {
                return this._mahh;
            }
            set
            {
                this._mahh = value;
            }
        }
        public double soluong
        {
            get
            {
                return this._soluong;
            }
            set
            {
                this._soluong = value;
            }
        }
        public DateTime ngaynhap
        {
            get
            {
                return this._ngaynhap;
            }
            set
            {
                this._ngaynhap = value;
            }
        }
        public double dongia
        {
            get
            {
                return this._dongia;
            }
            set
            {
                this._dongia = value;
            }
        }
        public double thanhtien
        {
            get
            {
                return this._thanhtien;
            }
            set
            {
                this._thanhtien = value;
            }
        }
        public int nhan
        {
            get
            {
                return this._nhan;
            }
            set
            {
                this._nhan = value;
            }
        }
        public int makho
        {
            get
            {
                return this._makho;
            }
            set
            {
                this._makho = value;
            }
        }
        public bool hangdu
        {
            get
            {
                return this._hangdu;
            }
            set
            {
                this._hangdu = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[11];
            paraName[0] = "@id_chitiet_nhapkho";
            paraName[1] = "@id_lannhap";
            paraName[2] = "@mahh";
            paraName[3] = "@soluong";
            paraName[4] = "@ngaynhap";
            paraName[5] = "@dongia";
            paraName[6] = "@thanhtien";
            paraName[7] = "@nhan";
            paraName[8] = "@makho";
            paraName[9] = "@hangdu";
            paraName[10] = "@option";
            object[] paraValue = new object[11];
            paraValue[0] = this._id_chitiet_nhapkho;
            paraValue[1] = this._id_lannhap;
            paraValue[2] = this._mahh;
            paraValue[3] = this._soluong;
            paraValue[4] = this._ngaynhap;
            paraValue[5] = this._dongia;
            paraValue[6] = this._thanhtien;
            paraValue[7] = this._nhan;
            paraValue[8] = this._makho;
            paraValue[9] = this._hangdu;
            paraValue[10] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 11, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_ERP_CHITIET_NHAPKHO", option);
        }

        
        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_CHITIET_NHAPKHO", "id_chitiet_nhapkho = " + this._id_chitiet_nhapkho);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_chitiet_nhapkho = Convert.ToInt32(ds.Tables[0].Rows[0]["id_chitiet_nhapkho"].ToString().Trim());
                this._id_lannhap = Convert.ToInt32(ds.Tables[0].Rows[0]["id_lannhap"].ToString().Trim());
                this._mahh = Convert.ToInt32(ds.Tables[0].Rows[0]["mahh"].ToString().Trim());
                this._soluong = Convert.ToDouble(ds.Tables[0].Rows[0]["soluong"].ToString().Trim());
                this._ngaynhap = Convert.ToDateTime(ds.Tables[0].Rows[0]["ngaynhap"].ToString().Trim());
                this._dongia = Convert.ToDouble(ds.Tables[0].Rows[0]["dongia"].ToString().Trim());
                this._thanhtien = Convert.ToDouble(ds.Tables[0].Rows[0]["thanhtien"].ToString().Trim());
                this._nhan = Convert.ToInt32(ds.Tables[0].Rows[0]["nhan"].ToString().Trim());
                this._makho = Convert.ToInt32(ds.Tables[0].Rows[0]["makho"].ToString().Trim());
                this._hangdu = Convert.ToBoolean(ds.Tables[0].Rows[0]["hangdu"].ToString().Trim());
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        public int Xoahangdu()
        {
            int i = 0;
            csCSDL csdl = new csCSDL();
            try
            {
                string sql = "delete from ERP_CHITIET_NHAPKHO where id_chitiet_nhapkho = " + this._id_chitiet_nhapkho;
                i = csdl.ExcuteSQL(sql);
                return i;
            }
            catch
            {
                return -1;
            }
        }
        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select row_number() over (order by c.tenhh) AS stt, c.tenhh, a.soluong*a.nhan as soluong, a.ngaynhap, a.dongia, a.thanhtien*a.nhan as thanhtien, b.donvitinh "
                + " from ERP_CHITIET_NHAPKHO a, ERP_DVT b, ERP_HANGHOA c"
                + " where a.id_lannhap = " + this._id_lannhap
                + " and a.mahh = c.id_hh and c.dvt = b.id_dvt and a.soluong > 0";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Tonkho()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select c.id_hh, b.donvitinh, d.tennhomhh, c.tenhh, sum(a.soluong*a.dongia*a.nhan) as tien, sum(a.soluong*a.nhan) as soluong"
                + " from ERP_CHITIET_NHAPKHO a, ERP_DVT b, ERP_HANGHOA c, ERP_NHOMHANG d, ERP_NHAPKHO e"
                + " where a.id_lannhap = e.id_lannhap and e.trangthai = 1 and a.mahh = c.id_hh and d.id_nhomhhh = c.id_nhom and c.dvt = b.id_dvt and a.makho = " + this._makho
                + " group by c.id_hh, b.donvitinh, d.tennhomhh, c.tenhh";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Tonkho_Truocngay(int ngay)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select c.id_hh, b.donvitinh, d.tennhomhh, c.tenhh, sum(a.soluong*a.dongia*a.nhan) as tien, sum(a.soluong*a.nhan) as soluong"
                + " from ERP_CHITIET_NHAPKHO a, ERP_DVT b, ERP_HANGHOA c, ERP_NHOMHANG d, ERP_NHAPKHO e"
                + " where a.id_lannhap = e.id_lannhap and e.trangthai = 1 and a.mahh = c.id_hh and d.id_nhomhhh = c.id_nhom and c.dvt = b.id_dvt and a.makho = " + this._makho
                + " and year(a.ngaynhap)*12*30 + month(a.ngaynhap)*30 + day(a.ngaynhap) <= " + ngay
                + " group by c.id_hh, b.donvitinh, d.tennhomhh, c.tenhh";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Tonkho_canhbao()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select c.id_hh, b.donvitinh, d.tenkho, c.tenhh, sum(a.soluong*a.dongia*a.nhan) as tien, sum(a.soluong*a.nhan) as soluong"
                + " from ERP_CHITIET_NHAPKHO a, ERP_DVT b, ERP_HANGHOA c, ERP_KHOHANG d, ERP_NHAPKHO e"
                + " where a.id_lannhap = e.id_lannhap and e.trangthai = 1 and a.mahh = c.id_hh and d.id_khohang = a.makho and c.dvt = b.id_dvt "
                + " group by c.id_hh, b.donvitinh, d.tenkho, c.tenhh"
                + " having sum(a.soluong*a.nhan) <= 10";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }
    }
}